package com.tuyan.aixiang.activity.config;

import android.content.Intent;
import android.net.wifi.ScanResult;

import com.flyco.dialog.widget.NormalListDialog;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.activity.add.AddDeviceActivity;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.lang.ListHelper;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.StringUtil;
import com.tuyan.aixiang.util.ToastUtil;
import com.tuyan.aixiang.util.WifiUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.AppCompatEditText;
import butterknife.BindView;
import butterknife.OnClick;

public class SmartConfigActivity extends BaseActivity<SmartConfigPresenter> implements SmartConfig.View{

    @BindView(R.id.et_wifi_name)
    AppCompatEditText etWifiName;

    @BindView(R.id.et_wifi_pass)
    AppCompatEditText etWifiPass;

    YsDevice ysDevice;

    @Override
    public void bindView() {
        super.bindView();
        Intent intent = getIntent();
        YsDevice device = (YsDevice) intent.getSerializableExtra("device");
        if(null == device) {
            finish();
            return;
        }
        ysDevice = device;
        iPresenter.init(device);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smart_config;
    }

    @Override
    protected SmartConfigPresenter createPresenter() {
        return new SmartConfigPresenter();
    }

    @OnClick(R.id.btn_device_connect)
    public void deviceConnect() {

        String ssid = getFormText(etWifiName);
        String pass = getFormText(etWifiPass);
        if(StringUtil.isBlank(ssid)) {
            ToastUtil.makeShort("请选择接入WiFi");
            return;
        }
        showLoading(true);
        iPresenter.deviceConnect(ssid, pass);
    }

    @OnClick(R.id.et_wifi_name)
    public void selectWifi() {
        if(null == WifiUtil.getInstance().getCurrentWifi()) {
            ToastUtil.makeShort("请先打开手机WiFi开关");
            WifiUtil.getInstance().openSetting(this);
            return;
        }

        List<ScanResult> wifis = WifiUtil.getInstance().getWifiList();
        if(wifis.size() == 0) {
            ToastUtil.makeLong("未获取到wifi列表,请打开定位服务和wifi开关后重试");
            WifiUtil.getInstance().openSetting(this);
            return;
        }
        List<String> wifiNames = new ArrayList<>();
        if(null != wifis && wifis.size() > 0) {
            ListHelper.forEach(wifis, w -> {
                wifiNames.add(w.SSID);
            });
        }
        NormalListDialog wifiListDialog = new NormalListDialog(this, wifiNames.toArray(new String[wifiNames.size()]));
        wifiListDialog.isTitleShow(false)
                .itemTextColor(getResources().getColor(R.color.textDark))
                .layoutAnimation(null)
                .show();
        wifiListDialog.setOnOperItemClickL((parent, view, position, id) -> {
            String wifiName = wifiNames.get(position);
            etWifiName.setText(wifiName);
            wifiListDialog.hide();
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @Override
    public void configSuccess() {
        runOnUiThread(() -> {
            hideLoading();
            Intent intent = new Intent(this, AddDeviceActivity.class);
            intent.putExtra("device", ysDevice);
            startActivity(intent);
            finish();
        });
    }
    @Override
    public void configFail() {
        runOnUiThread(() -> {
            hideLoading();
            ToastUtil.makeShort("设备配网超时. 请重试");
            finish();
        });
    }
}